//
//  XCMainTabBar.swift
//  小陈微博Swift用纯代码创建
//
//  Created by 小陈 on 16/2/26.
//  Copyright © 2016年 小陈. All rights reserved.
//

import UIKit

protocol XCMainViewControllerDelegate : NSObjectProtocol {
  
    func ClickCenterButton()

}
@objc protocol XCVisitorViewDelegate : NSObjectProtocol {
    optional
/// 定义了一个可选的代理方法
    func MainTabBarButtonClick()
    
}



class XCMainTabBar: UITabBar {

    // 第一代理属性
    weak var TabBarButtonDelegate: XCVisitorViewDelegate?
    weak var ClickCenterButtonDelegate: XCMainViewControllerDelegate?
    let btnCount:CGFloat = 5
    override func layoutSubviews() {
        super.layoutSubviews()
        
       var index = 0
        let w = bounds.size.width / (CGFloat)(btnCount)
        let h = bounds.size.height
        let frame = CGRectMake(0, 0, w, h)
        
        for v in subviews{
            if v is UIControl && !(v is UIButton){ // 遍历TabBar上
                v.frame = CGRectOffset(frame, CGFloat(index) * w, 0)
                index += (index == 1) ? 2 :1
            }
        }
        
      composeButton.frame = frame
        composeButton.center = CGPointMake(self.center.x , h * 0.5)
        
    }
    // 插入中间的按钮
   lazy var composeButton: UIButton = {
        let btn = UIButton()
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState:UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
         btn.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        self.addSubview(btn)
     
        return btn
    }()
    func buttonClick(button:UIButton){

        print("你点击了中间的按钮\(button)")
        // 点击了中间的 按钮实现的代理方法
       ClickCenterButtonDelegate?.ClickCenterButton()
        
        
        
        
        
    }
}
